﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace RenShiPinYongBiao
{
    class ConnectionManager
    {
        private static SqlConnection con;

        public static SqlConnection Connection
        {
            get { return ConnectionManager.con; }
        }
        private static String conString;
        private static void BuildConnectionString()
        {
            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
            builder.DataSource = "woutsrv";
            builder.InitialCatalog = "hrdb";
            builder.PersistSecurityInfo = false;
            builder.UserID = "ybj";
            builder.Password = "0813rs#";
            conString = builder.ConnectionString;
        }
        public static void OpenConnection()
        {
            if (con == null)
            {
                BuildConnectionString();
                con = new SqlConnection(conString);
            }
            try
            {
                con.Open();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("无法连接到数据库："+ex.Message);
                Application.Exit();
            }
        }
        public static void CloseConnection()
        {
            try
            {
                con.Close();
            }
            catch (SqlException e)
            {
                MessageBox.Show(e.Message);
            }
        }
    }
}
